<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/3/8
 * Time: 17:26
 */

namespace App\Rep;


use App\Models\ModelNumber;
use App\Models\ModelNumberSpecification;
use App\Models\ModelNumberUser;
use App\Models\Product;
use App\Models\ProductCenter;
use App\Models\ProductJobType;
use App\Models\ProductModelNumber;
use Illuminate\Support\Facades\DB;

class ModelNumberRep
{

    /**
     * 获取型号列表
     * @param $params
     * @return mixed
     */
    public function getModelNumberListRep($params){
        if(isset($params['product_id']) && !empty($params['product_id'])){
            $id_arr = ProductModelNumber::where('product_id',$params['product_id'])
                ->pluck('model_number_id')
                ->toArray();
            $select_arr = "*";
            if(!empty($params['field_list'])){
                $select_arr = explode(',',$params['field_list']);
            }else{
                $select_arr = "*";
            }
            $model = ModelNumber::whereIn('id',$id_arr)->where(function($q)use($params){
                if(!empty($params['model_number_id'])){
                    $q->where('id',$params['model_number_id']);
                }
            });
            if(isset($params['is_dropdown']) && !empty($params['is_dropdown'])){
                if($params['file'] == 1){
                    array_push($select_arr,'file_id');
                    $model->with('file');
                }
                $list = $model->select($select_arr)
                    ->orderBy('model_number','asc')
                    ->paginate($params['per_page']??1000);
            }else{
                if($params['file'] == 1){
                    array_push($select_arr,'file_id');
                    $model->with('file');
                }
                $list = $model->with(['product','specification'])->select($select_arr)
                    ->orderBy('model_number','asc')
                    ->paginate($params['per_page']??1000);
            }
        }else{
            $list = ModelNumber::Search($params)->with(['product','specification','file'])
                ->where('is_del',2)
                ->orderBy('model_number','asc')
                ->paginate($params['per_page']??1000);
        }

        return $list;
    }

    /**
     * 添加型号
     * @param $params
     * @return ModelNumber|array
     */
     public function addModelNumberRep($params){
         DB::beginTransaction();
         try {
             if(isset($params['model_number_id']) && !empty($params['model_number_id'])){
                 ModelNumber::where('id',$params['model_number_id'])->update(['model_number'=>$params['model_number'],
                     'file_id'=>$params['file_id'],'remark'=>$params['remark']??'']);
                 foreach($params['product_ids'] as $k=>$v){
                     ProductModelNumber::updateOrCreate(['product_id'=>$v,'model_number_id'=>$params['model_number_id']],['product_id'=>$v,'model_number_id'=>$params['model_number_id']]);
                 }
             }else{
                 $obj = ModelNumber::updateOrCreate(
                     ['model_number'=>$params['model_number']],
                     ['model_number'=>$params['model_number'],'file_id'=>$params['file_id'],'remark'=>$params['remark']??'']
                 );
                 foreach($params['product_ids'] as $k=>$v){
                     ProductModelNumber::updateOrCreate(['product_id'=>$v,'model_number_id'=>$obj->id],['product_id'=>$v,'model_number_id'=>$obj->id]);
                 }
             }

             DB::commit();
             return ['code'=>0,'data'=>[],'msg'=>''];
         }catch(\Exception $e){
             DB::rollBack();
             return ['code'=>2001,'data'=>[],'msg'=>$e->getMessage()];
         }
     }
    /**
     * 产品关联型号
     * @param $params
     * @return array
     */
    public function createModelNumberRep($params){
        DB::beginTransaction();
        try {
            if (isset($params['model_number_id']) && !empty($params['model_number_id'])) {
                $model = ModelNumber::where('id', $params['model_number_id'])->first();
            } else {
                $model = new ModelNumber();
            }
            //p($model->toArray());
            $model->model_number = $params['model_number'];
            if (isset($params['file_id']) && !empty($params['file_id'])) {
                $model->file_id = $params['file_id'];
            }
            $model->save();
            $product_id = $params['product_id'];
            ProductModelNumber::updateOrCreate(
                ['model_number_id'=>$model->id,'product_id'=>$product_id],
                ['model_number_id'=>$model->id,'product_id'=>$product_id]
            );
            DB::commit();
            return ['code'=>0,'data'=>$model,'msg'=>''];
        }catch(\Exception $e){
            DB::rollBack();
            return ['code'=>2001,'data'=>[],'msg'=>$e->getMessage()];
        }
    }

    /**
     * 删除型号
     * @param $params
     */
    public function delModelNumberRep($params){
        $model = ModelNumber::where('id',$params['id'])->first();
        $model->is_del = 1;
        $model->save();
//        $where = ['model_number_id'=>$params['id'],'product_id'=>$params['product_id']];
//        ProductModelNumber::where($where)->delete();
        ProductModelNumber::where('model_number_id',$params['id'])->delete();
        ModelNumberSpecification::where('model_number_id',$params['id'])->delete();
        return $model;
    }

    public function delProductModelnumberRep($params){
        $where = ['model_number_id'=>$params['model_number_id'],'product_id'=>$params['product_id']];
        ProductModelNumber::where($where)->delete();
        return true;
    }


    /**
     * 批量添加型号
     * @param $params
     */
    public function batchAddModelNumberRep($params){
        $array = $params['model_number_ids'];
        foreach($array as $k=>$v){
            $where = ['model_number_id'=>$v,'product_id'=>$params['product_id']];
            ProductModelNumber::updateOrCreate($where,$where);
        }
        return ['code'=>0,'data'=>[],'msg'=>''];
    }

    //型号-工序关联员工
    public function setModelNumberUserRep($params){
        foreach($params['param_data'] as $k=>$v){
            if(!empty($v['users'])){
                ModelNumberUser::where('model_number_id',$v['model_number_id'])
                    ->where('job_type_id',$v['job_type_id'])->delete();
                foreach($v['users'] as $kk=>$vv){
                    $where = [
                        'model_number_id'=>$v['model_number_id'],
                        'job_type_id'=>$v['job_type_id'],
                        'user_id'=>$vv
                    ];

                    $obj = ModelNumberUser::updateOrCreate(
                        $where,$where
                    );
                }
            }else{
                $where = [
                    'model_number_id'=>$v['model_number_id'],
                    'job_type_id'=>$v['job_type_id'],
                    'user_id'=>''
                ];
                ModelNumberUser::where('model_number_id',$v['model_number_id'])
                    ->where('job_type_id',$v['job_type_id'])->delete();
                $obj = ModelNumberUser::updateOrCreate(
                    $where,$where
                );
            }
        }

        return $obj;
    }

    public function getModelNumberUserRep($params){
        $job_type_list = (new ProductRep())->getProductJobTypeListByProductId($params['product_id']);
        $list = [];
        foreach($job_type_list as $k=>$v){
            $list[$k]['job_type'] = $v;
             $arr = ModelNumberUser::where('model_number_id',$params['model_number_id'])
                ->where('job_type_id',$v['id'])
                ->pluck('user_id')->toArray();
             if(!empty($arr)){
                 if($arr[0] == 0){
                     $arr = [];
                 }
             }

            $list[$k]['user_id_arr'] = $arr;
        }
        return $list;
    }

    public function getUserListByModelNumberIdRep($params){
        return ModelNumberUser::where('model_number_id',$params['model_number_id'])
            ->where('job_type_id',$params['job_type_id'])
            ->with(['user'])
            ->get();
    }

}
